﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Data;
using MyWeb.Business;
namespace MyWeb.Admins
{
    public partial class tblDiscount : System.Web.UI.Page
    {
        static bool Insert = false; 
        protected void Page_Load(object sender, EventArgs e) 
        {
            if (!IsPostBack) 
            {
                Session["folder"] = "tblDiscount"; BindGrid();
            }
        }
        private void BindGrid() 
        { 
            grdtblDiscount.DataSource = tblDiscountService.tblDiscount_GetByAll();
            grdtblDiscount.DataBind(); 
        }
        protected void btnThem_Click(object sender, EventArgs e) 
        { 
            pnShow.Visible = false; pnUpdate.Visible = true; 
            ControlClass.ResetControlValues(this);
            Session["upload"] = null; 
            Insert = true; 
        }
        protected void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdtblDiscount.Items.Count; i++)
                {
                    item = grdtblDiscount.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item) 
                    { 
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked) 
                        {
                            string strId = item.Cells[1].Text; 
                            try 
                            {
                                tblDiscountService.tblDiscount_Delete(strId);
                            } 
                            catch
                            { 
                            }
                        } 
                    } 
                } grdtblDiscount.CurrentPageIndex = 0; BindGrid();
            }
            catch 
            { 
            } 
        }
        protected void grdtblDiscount_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Data.tblDiscountInfo strCA = new tblDiscountInfo();
            strCA._did = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                DataTable dt = tblDiscountService.tblDiscount_GetById(strCA);
                dll_sid_Update.SelectedValue = dt.Rows[0]["_sid"].ToString();
                txt_did.Text = dt.Rows[0]["_did"].ToString();
                txt_drate.Text = dt.Rows[0]["_drate"].ToString();
                pnUpdate.Visible = true;
                pnShow.Visible = false;
            }
            else { tblDiscountService.tblDiscount_Delete(strCA._did); BindGrid(); }
        }
        protected void lblBack_Click(object sender, EventArgs e)
        { 
            pnUpdate.Visible = false;
            pnShow.Visible = true; 
            Insert = false; }
        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();
            #region[TestInput]
            if (dll_sid_Update.SelectedValue.Trim().Equals(""))
            { 
                WebMsgBox.Show("_sid không thể trống !");
                dll_sid_Update.Focus();
                return;
            } 
            if (txt_did.Text.Trim().Equals("")) 
            {
                WebMsgBox.Show("_did không thể trống !");
                txt_did.Focus(); 
                return; 
            }
            if (txt_drate.Text.Trim().Equals(""))
            { 
                WebMsgBox.Show("_drate không thể trống !"); 
                txt_drate.Focus(); 
                return; }
            #endregion
            Data.tblDiscountInfo obj = new Data.tblDiscountInfo();
            obj._sid = dll_sid_Update.SelectedValue;
            obj._did = txt_did.Text; 
            obj._drate = txt_drate.Text;
            if (Insert == true)
            { 
                tblDiscountService.tblDiscount_Insert(obj); 
            } 
            else
            { 
                tblDiscountService.tblDiscount_Update(obj);
            }
            BindGrid();
            pnShow.Visible = true; 
            pnUpdate.Visible = false; 
            Insert = false;
        }
        protected void grdtblDiscount_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        { 
            grdtblDiscount.CurrentPageIndex = e.NewPageIndex; 
            BindGrid();
        }
        private void ShowDll()
        {
            var dt = new DataTable();
            dt = tblImgSizeService.tblImgSize_GetByAll();
            dll_sid.DataSource = dt; 
            dll_sid.DataTextField = ""; 
            dll_sid.DataValueField = "";
            dll_sid.DataBind(); 
            dll_sid_Update.DataSource = dt;
            dll_sid_Update.DataTextField = "";
            dll_sid_Update.DataValueField = ""; 
            dll_sid_Update.DataBind();
        }
        
        protected void btnshow_Click(object sender, EventArgs e) 
        {
            grdtblDiscount.DataSource = tblDiscountService.tblDiscount_GetBy_sid(dll_sid.SelectedValue); grdtblDiscount.DataBind(); }
        protected void grdtblDiscount_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll"); 
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdtblDiscount.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")"); ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
                    }
                }
            }
        }
    }
}